package com.jastar.android.test.demo;

import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.jastar.android.test.R;
import com.jastar.android.tts.BaiduTTSConfig;
import com.jastar.android.tts.BaiduTTSManager;
import com.jastar.android.util.*;

import java.util.ArrayList;
import java.util.List;

public class WordsStudyActivity extends AppCompatActivity implements View.OnClickListener {

    private List<String> words;
    private List<String> wordsCN;
    private String currentWordCN;
    private TextView tv_word;
    private ImageView iv_word;
    private int audioId;
    private View button_next;
    private View button_play;

    private BaiduTTSManager ttsManager;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words_study);

        initWords();

        tv_word = findViewById(R.id.tv_word);
        iv_word = findViewById(R.id.iv_word);
        button_play = findViewById(R.id.button_play);
        button_next = findViewById(R.id.button_next);
        button_play.setEnabled(false);
        button_next.setEnabled(false);

        ttsManager = BaiduTTSManager.initInstance(this);
        handler = new Handler();

        resizeView();
        nextWord();

        button_play.setOnClickListener(this);
        button_next.setOnClickListener(this);
    }

    private void initWords() {
        words = new ArrayList<>();
        wordsCN = new ArrayList<>();
        for (String line : FileUtil.readRawLines(getResources(), R.raw.words)) {
            String[] parts = line.split("\\|");
            words.add(parts[0]);
            wordsCN.add(parts[1]);
        }
    }

    @Override
    public void onClick(View v) {
        button_play.setEnabled(false);
        button_next.setEnabled(false);
        if (v.getId() == R.id.button_play) {
            AudioPlayer.getPlayer().playOnce(this, audioId, this.audioCompletionListener);
        } else if (v.getId() == R.id.button_next) {
            nextWord();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        AudioPlayer.getPlayer().stop();
    }

    private final BaseListener<Integer> audioCompletionListener = audioId -> handler.postDelayed(() -> ttsManager.speak(currentWordCN, BaiduTTSConfig.PERS[1], new BaiduTTSManager.TTSListener() {
        @Override
        public void onDone() {
            button_play.setEnabled(true);
            button_next.setEnabled(true);
        }

        @Override
        public void onError(String error) {
            button_play.setEnabled(true);
            button_next.setEnabled(true);
        }
    }), 1000);

    private void resizeView() {
        Point point = Utils.getActivityDisplaySize(this);
        int len = Math.min(point.x, point.y) / 2;
        ViewGroup.LayoutParams params = iv_word.getLayoutParams();
        params.width = len;
        params.height = len;
        iv_word.setLayoutParams(params);
    }

    private void nextWord() {
        int index = RandomUtil.random.nextInt(words.size());
        String word = words.get(index);
        currentWordCN = wordsCN.get(index);
        String name = WordsActivity.getNameByWord(word);
        tv_word.setText(word);
        iv_word.setImageResource(getDrawableByName(name));

        audioId = getRawByName(name);
        AudioPlayer.getPlayer().playOnce(this, audioId, this.audioCompletionListener);
    }

    private int getDrawableByName(String name) {
        return getResources().getIdentifier(name, "drawable", getPackageName());
    }

    private int getRawByName(String name) {
        return getResources().getIdentifier(name, "raw", getPackageName());
    }

}